import Vue from 'vue';
import Vuex from 'vuex';
import { api } from '../Axios';
import _ from 'lodash';

let playerImages = [
  require('@/assets/icon/骑士.png'),
  require('@/assets/icon/刺客.png'),
  require('@/assets/icon/法师.png'),
];

Vue.use(Vuex);

export default new Vuex.Store({
  modules: {},
  state: {
    TOKEN: null,
    msg: { name: 'a123' },
    // prettier-ignore
    levels:[
      { threshold: 0, name: '入门学徒' },
      { threshold: 100, name: '青铜会员' },
      { threshold: 500, name: '白银会员' },
      { threshold: 1000, name: '黄金会员' },
      { threshold: 5000, name: '铂金会员' },
      { threshold: 10000, name: '钻石会员' },
      { threshold: Infinity, name: '至尊王者' }, // 兜底处理
    ],
    items: [],
    playerImage: playerImages[0],
  },
  getters: {
    // getValue: (state) => (key) => {
    //   return state[key];
    // },
    getLevel: (state) => (points) => {
      // points 通常指积分/经验什么的
      // 获取当前等级
      const currentLevel = state.levels.find(
        (level) => points >= level.threshold
      );
      const currentIndex = state.levels.indexOf(currentLevel);
      // 获取下一等级
      const nextLevel = state.levels[currentIndex + 1];
      // 计算进度百分比
      let progress = 0;
      if (nextLevel && nextLevel.threshold !== Infinity) {
        const range = nextLevel.threshold - currentLevel.threshold;
        progress = (points - currentLevel.threshold) / range;
        progress = Math.min(1, Math.max(0, progress)); // 限制在0-100%之间
      } else {
        progress = 1; // 已达到最高等级
      }

      return {
        level: currentLevel.name,
        progress: progress * 100, // 转换为百分比
        currentThreshold: currentLevel.threshold,
        nextThreshold: (nextLevel && nextLevel.threshold) || Infinity,
      };
    },
  },
  mutations: {
    setValue: (state, obj) => {
      state[obj.key] = obj.value;
    },
  },
  actions: {
    fetchCusInfo: (ctx, payload) => {},
    fetchItems: (ctx, payload) => {},
  },
});
